package com.cyandpos.pad.activity;

import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import com.cyandpos.pad.R;
import com.cyandpos.pad.presentation.AdPresentationUtil;
import com.cyandpos.pad.utils.ToastUtils;


/**
 * Created by Administrator on 2018/05/15.
 */

public class LockActivity extends BaseActivity {
    private static final String FOCUS_FLAG_USERNAME = "etUsername";
    private static final String FOCUS_FLAG_PASSWORD = "etPassword";

    private AutoCompleteTextView etUsername;
    private EditText etPassword;
   // private Dialog_waiting dialog;
    private Button btnZero, btnOne, btnTwo, btnThree, btnFour, btnFive, btnSix, btnSeven, btnEight, btnNine, btnBack, btnSure, btnLogin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);//横屏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);// 设置全屏
}

    @Override
    protected void initData() {
        etUsername = (AutoCompleteTextView) this.findViewById(R.id.et_username_login_act);
        etPassword = (EditText) this.findViewById(R.id.et_password_login_act);

        etUsername.setSelected(true);
        etUsername.setOnClickListener(new CalOnClickListener());
        etPassword.setOnClickListener(new CalOnClickListener());

        btnLogin = (Button) this.findViewById(R.id.btn_login_login_act);
        btnZero = (Button) this.findViewById(R.id.btn_zero_login_act);
        btnOne = (Button) this.findViewById(R.id.btn_one_login_act);
        btnTwo = (Button) this.findViewById(R.id.btn_two_login_act);
        btnThree = (Button) this.findViewById(R.id.btn_three_login_act);
        btnFour = (Button) this.findViewById(R.id.btn_four_login_act);
        btnFive = (Button) this.findViewById(R.id.btn_five_login_act);
        btnSix = (Button) this.findViewById(R.id.btn_six_login_act);
        btnSeven = (Button) this.findViewById(R.id.btn_seven_login_act);
        btnEight = (Button) this.findViewById(R.id.btn_eight_login_act);
        btnNine = (Button) this.findViewById(R.id.btn_nine_login_act);
        btnBack = (Button) this.findViewById(R.id.btn_back_login_act);
        btnSure = (Button) this.findViewById(R.id.btn_sure_login_act);

        btnZero.setOnClickListener(new CalOnClickListener());
        btnOne.setOnClickListener(new CalOnClickListener());
        btnTwo.setOnClickListener(new CalOnClickListener());
        btnThree.setOnClickListener(new CalOnClickListener());
        btnFour.setOnClickListener(new CalOnClickListener());
        btnFive.setOnClickListener(new CalOnClickListener());
        btnSix.setOnClickListener(new CalOnClickListener());
        btnSeven.setOnClickListener(new CalOnClickListener());
        btnEight.setOnClickListener(new CalOnClickListener());
        btnNine.setOnClickListener(new CalOnClickListener());
        btnBack.setOnClickListener(new CalOnClickListener());
        btnSure.setOnClickListener(new CalOnClickListener());
        btnLogin.setOnClickListener(new CalOnClickListener());
        btnBack.setOnLongClickListener(new BackOnLongClickListener());

      /*  dialog = new Dialog_waiting(LockActivity.this);
        dialog.setCancelable(false);
        dialog.setMessage("Loading ....");
        dialog.isWaitDialog(true);*/

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_lock;
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK: {
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    public void Login(String uname, String pword) {

        SharedPreferences sp = getSharedPreferences("USER", MODE_PRIVATE);
        String name = sp.getString("user", null);
        String pwd = sp.getString("pwd", null);

        if (null == name || pwd == null) {
            // TODO: 2021/06/01  网络请求
        }

        if (uname.trim().endsWith(name) && pword.trim().endsWith(pwd)) {
            this.finish();
        } else {
            ToastUtils.showMsg("帐号/密码错误或不匹配");

        }

    }


    class BackOnLongClickListener implements View.OnLongClickListener {

        @Override
        public boolean onLongClick(View view) {
            if (view.getId() == R.id.btn_back_login_act) {
                calLongBack();
            }
            return false;
        }
    }

    class CalOnClickListener implements View.OnClickListener {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {

                case R.id.et_username_login_act:
                    etUsername.setSelected(true);
                    etPassword.setSelected(false);
                    break;

                case R.id.et_password_login_act:
                    etPassword.setSelected(true);
                    etUsername.setSelected(false);
                    break;

                case R.id.btn_login_login_act:
                    Login(etUsername.getText().toString().trim(), etPassword.getText().toString().trim());
                    break;
                case R.id.btn_zero_login_act:
                    calInput("0");
                    break;
                case R.id.btn_one_login_act:
                    calInput("1");
                    break;
                case R.id.btn_two_login_act:
                    calInput("2");
                    break;
                case R.id.btn_three_login_act:
                    calInput("3");
                    break;
                case R.id.btn_four_login_act:
                    calInput("4");
                    break;
                case R.id.btn_five_login_act:
                    calInput("5");
                    break;
                case R.id.btn_six_login_act:
                    calInput("6");
                    break;
                case R.id.btn_seven_login_act:
                    calInput("7");
                    break;
                case R.id.btn_eight_login_act:
                    calInput("8");
                    break;
                case R.id.btn_nine_login_act:
                    calInput("9");
                    break;
                case R.id.btn_back_login_act:
                    calBack();
                    break;
                case R.id.btn_sure_login_act:
                    calSure();
                    break;
                default:
                    break;
            }
        }
    }


    private String getEditFocus() {
        if (etUsername.isSelected()) {
            return FOCUS_FLAG_USERNAME;
        } else if (etPassword.isSelected()) {
            return FOCUS_FLAG_PASSWORD;
        }
        return null;
    }

    private void calInput(String num) {
        String temp = getEditFocus();
        if (temp.equals(FOCUS_FLAG_USERNAME)) {
            etUsername.append(num);
        } else if (temp.equals(FOCUS_FLAG_PASSWORD)) {
            etPassword.append(num);
        }
    }


    private void calBack() {
        String temp = getEditFocus();
        if (temp.equals(FOCUS_FLAG_USERNAME)) {
            int index = etUsername.getSelectionStart();
            String str = etUsername.getText().toString();
            if (!str.equals("") && index > 0) {
                etUsername.getText().delete(index - 1, index);
            }

        } else if (temp.equals(FOCUS_FLAG_PASSWORD)) {
            int index = etPassword.getSelectionStart();
            String str = etPassword.getText().toString();
            if (!str.equals("") && index > 0) {
                etPassword.getText().delete(index - 1, index);
            }
        } else {

        }
    }

    private void calLongBack() {
        String temp = getEditFocus();
        if (temp.equals(FOCUS_FLAG_USERNAME)) {
            if (!etUsername.getText().toString().equals("")) {
                etUsername.setText("");
            }
        } else if (temp.equals(FOCUS_FLAG_PASSWORD)) {
            if (!etPassword.getText().toString().equals("")) {
                etPassword.setText("");
            }
        }
    }

    private void calSure() {
        if (getEditFocus().equals(FOCUS_FLAG_USERNAME)) {
            etPassword.setSelected(true);
            etUsername.setSelected(false);
        } else if (getEditFocus().equals(FOCUS_FLAG_PASSWORD)) {
            if (etPassword.getText().toString().equals("")) {
                etUsername.setSelected(true);
                etPassword.setSelected(false);
            } else {
                Login(etUsername.getText().toString().trim(), etPassword.getText().toString().trim());
            }
        }
    }


}

